﻿create PROCEDURE Sync.SP_wfProcessInstances_SelectChanges
@LastRowVersion rowversion,
@FilterdColumn UNIQUEIDENTIFIER
AS 
BEGIN 
	SELECT 
        t.[ProcessInstanceID],
		g.[ProcessID],g.[UserID],g.[CreationDate],g.[CompletionDate],g.[EntityID],
		t.IsDeleted,
		t.LastUpdatedDate
	FROM [dbo].[wfProcessInstances] g RIGHT JOIN Sync.wfProcessInstances_Tracking t  
    ON t.ProcessInstanceID = g.ProcessInstanceID 
	WHERE (@LastRowVersion IS NULL OR t.Version > @LastRowVersion)
	AND ((@FilterdColumn IS NULL) OR
		(g.ProcessInstanceID in 
		(SELECT     wfProcessInstances.ProcessInstanceID
		FROM        wfProcessInstances INNER JOIN
                    CustomerLoans ON wfProcessInstances.EntityID = CustomerLoans.LoanID
		WHERE     (CustomerLoans.CommitteeID = @FilterdColumn))
		OR g.ProcessInstanceID in
		(SELECT     wfProcessInstances.ProcessInstanceID
		FROM         wfProcessInstances INNER JOIN
                      Board ON wfProcessInstances.EntityID = Board.BoardID
		WHERE     (Board.CommitteeID = @FilterdColumn))))
    END











